package com.lili.collection.restful.config;

import cn.hutool.core.text.CharSequenceUtil;
import com.lili.collection.restful.base.ParameterDef;
import com.lili.collection.restful.enums.InterfaceType;
import com.lili.collection.restful.enums.RequestMethodType;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.pulsar.shade.com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

/**
 * 接口配置
 *
 * @author: lili
 * @since: 2023/2/23 19:45
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class InterfaceConfig extends BaseDsConfig {

    /**
     * url
     */
    @JsonProperty(value = "Url")
    private String url;

    /**
     * 接口类型
     * @see InterfaceType
     */
    @JsonProperty(value = "InterfaceType")
    private String interfaceType;

    /**
     * 请求方法类型
     *
     * @see RequestMethodType
     */
    @JsonProperty(value = "RequestMethodType")
    private String requestMethodType;

    /**
     * 解析路径
     */
    @JsonProperty(value = "ParsePath")
    private String parsePath;

    /**
     * 请求头
     */
    @JsonProperty(value = "ReqHeader")
    private String reqHeader;


    /**
     * 请求数据类型0自适应1数组2对象
     */
    @JsonProperty(value = "ReqDataType")
    private String reqDataType;

    /**
     * 请求传参方式Path Query Body
     */
    @JsonProperty(value = "ReqTransMode")
    private String reqTransMode;

    /**
     * 请求参数
     */
    @JsonProperty(value = "ReqParams")
    private List<ParameterDef> reqParams;

    /**
     * 配置参数
     */
    @JsonProperty(value = "Properties")
    private String properties;

    /**
     * 返回码信息
     */
    @JsonProperty(value = "ReturnCodeFlag")
    private String returnCodeFlag;
    /**
     * 返回码字段
     */
    @JsonProperty(value = "ReturnCode")
    private String returnCode;
    /**
     * 返回码成功标志
     */
    @JsonProperty(value = "ReturnSuccessFlag")
    private String returnSuccessFlag;
    /**
     * 返回说明字段
     */
    @JsonProperty(value = "ReturnMessageCode")
    private String returnMessageCode;


    @Override
    public String getPzbs() {
        return this.url + this.requestMethodType;
    }

    @Override
    public void init() {
        super.init();
        //参数校验
        if (CharSequenceUtil.hasBlank(this.url, this.interfaceType, this.requestMethodType)) {
            throw new IllegalArgumentException("接口url、类型、请求方式都不能为空！");
        }
        this.interfaceType = InterfaceType.codeOf(this.interfaceType).getCode();
        //暂时只有Restful会限制url
        if (InterfaceType.RESTFUL.getCode().equals(this.interfaceType) && CharSequenceUtil.isBlank(this.url)) {
            throw new IllegalArgumentException("接口url不能为空！");
        }
        this.requestMethodType = RequestMethodType.codeOf(this.requestMethodType).getCode();
    }
}
